'''
Created on 2011-2-9

@author: wilbur.ma
'''

import os, shutil

class Error(EnvironmentError):
    pass

class FDUtil(object):

    @classmethod
    def copytree(cls, src, dst, ignore=None):
        '''
        Truncated version of shutil.copytree.
        '''
        names = os.listdir(src)
        if ignore is not None:
            ignored_names = ignore(src, names)
        else:
            ignored_names = set()
    
        ## if dst not existent, create it
        if not os.path.exists(dst):
            os.makedirs(dst)
        errors = []
        for name in names:
            if name in ignored_names:
                continue
            srcname = os.path.join(src, name)
            dstname = os.path.join(dst, name)
            try:
                if os.path.isdir(srcname):
                    FDUtil.copytree(srcname, dstname, ignore)
                else:
                    # Will raise a SpecialFileError for unsupported file types
                    shutil.copy2(srcname, dstname)
            # catch the Error from the recursive copytree so that we can
            # continue with other files
            except Error as err:
                errors.extend(err.args[0])
            except EnvironmentError as why:
                errors.append((srcname, dstname, str(why)))
        try:
            shutil.copystat(src, dst)
        except OSError as why:
            if WindowsError is not None and isinstance(why, WindowsError):
                # Copying file access times may fail on Windows
                pass
            else:
                errors.extend((src, dst, str(why)))
        if errors:
            raise Error(errors)


def main():
    FDUtil.copytree('F:/Lib', 'F:/lib2')
    print('Done.')
    
if __name__ == '__main__':
    print(os.listdir('F:/Lib2'))
    main()